import Vue from "vue";
import VueRouter from "vue-router";
import routes from "./routers";
import store from "@/store";
import { getToken } from "@/utils/util";
import config from "@/config";
const { whitePage } = config;

Vue.use(VueRouter);

const router = new VueRouter({
  // mode: "history",
  mode: "hash",
  base: process.env.BASE_URL,
  routes
});
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

const LOGIN_PAGE_NAME = "Login";
//判断是否是有权限
const turnTo = (to,  next) => {
  if(store.state.user.userInfo.moduleList&&store.state.user.userInfo.moduleList.includes(to.meta.moduleId) || to.meta.navShow) next();
  // 有权限，可访问
  else next({ replace: true, name: "error_401" }); // 无权限，重定向到401页面
};

router.beforeEach((to, from, next) => {
  next();
  // const token = Vue.prototype.$cookies.get('usertoken');
  // // const token = Vue.prototype.$cookies.get('usertoken');
  // if(whitePage.indexOf(to.name) !== -1) return next();
  // if (!token && to.name !== LOGIN_PAGE_NAME) {
  //   // 未登录且要跳转的页面不是登录页
  //    next({
  //     name: LOGIN_PAGE_NAME // 跳转到登录页
  //   });
  // } else if (!token && to.name === LOGIN_PAGE_NAME) {
  //   // 未登陆且要跳转的页面是登录页
  //   next(); // 跳转
  // } else if (token && to.name === LOGIN_PAGE_NAME) {
  //   //已登录且要跳转的页面是登录页
  //   // let homeName = "Home";
  //   // next({
  //   //   name: homeName // 跳转到homeName页
  //   // });
  //   next();
  // } else {
  //   //页面权限配置
  //   turnTo(to, next);
  // }
});

export default router;
